

import 'dart:convert';

import 'package:bip39/bip39.dart';
import 'package:eosdart_ecc/eosdart_ecc.dart';
import 'package:secp256k1/secp256k1.dart';
import 'package:xim/http/ase.dart';
import 'package:wallet/wallet.dart' as wallet;

/// https://learnblockchain.cn/2018/09/28/hdwallet/
/// https://learnblockchain.cn/article/7783
/// https://learnblockchain.cn/article/7051
/// secp256k1: https://www.freebuf.com/articles/database/165851.html
/// format https://blog.csdn.net/qq_50680426/article/details/121445011
void main(){
  var mnemonic = generateMnemonic().split(" ").toList();
  final seed = wallet.mnemonicToSeed(mnemonic);
  final master = wallet.ExtendedPrivateKey.master(seed, wallet.xprv);
  var key = master.key;
  print("master private key: ${key.toRadixString(16)}");
  print("master public key: "
      "${master.publicKey.q.x},"
      " ${master.publicKey.q.y}}"
  );
  print(master.publicKey.toString());

  print("----------------------------");

  final root = master.forPath("m/44'/195'/0'/0/0");
  final privateKey = wallet.PrivateKey((root as wallet.ExtendedPrivateKey).key);
  final publicKey = wallet.tron.createPublicKey(privateKey);


  var pk = PrivateKey.fromHex('c37c299bb7f5ffd8d9329d052983342a8c3234ff3b3fa32a292187341f7146d7');
  var pub = pk.publicKey;
  print(pub.toHex());
  print(pub.toCompressedHex());

  print('--------------------');
  var msg = "aaaaa";
  var signature = pk.signature(msg);

  print(signature);

  // print(sig.verify(pub, messageHash)); // true

}

String  intToHex(int num) {
  String hexString = num.toRadixString(16);
  return hexString;
}